import gnu.regexp.*;
import java.net.*;
import java.io.*;

public class cnnCrawler {

	public static void main(String[] args) {
		String pageURL = "http://www.cnn.com";
		StringBuffer test = new StringBuffer(pageHtml(pageURL));
	    test = stripPageContents(test);
	    System.out.println(test);
	}
	
	public static StringBuffer pageHtml(String pageURL) {
		String cnnBaseInputLine; //new location
		StringBuffer convert = new StringBuffer(); //new location
		
		try {
		   URL cnnBaseDocument = new URL(pageURL);
		   cnnBaseDocument.openConnection();
		   BufferedReader cnnBaseBuffer = new BufferedReader(new InputStreamReader(cnnBaseDocument.openStream()));
		   //String cnnBaseInputLine; original
		   //StringBuffer tempDocument = new StringBuffer(); original
		   while((cnnBaseInputLine = cnnBaseBuffer.readLine()) != null)
		      convert.append(cnnBaseInputLine).append("\n"); //\n not included
		   cnnBaseBuffer.close();
		   //return(tempDocument); returns before access to exceptions
		}
	    catch(MalformedURLException e) {
		   System.out.println("unable to create URL object");
		   //return(null); don't return null
		}
		catch(IOException e) { //added exception
		   System.out.println("unable to create buffer object");
		   //return(null);
		}
		
		return (convert); //place return at the end of the function

	}
	
	public static StringBuffer stripPageContents(StringBuffer pageHtml) {
		//I moved declarations here without exceptions here
		int isolateLeftIndex = 0; //moved here. re-named variable
		int isolateRightIndex = 0; //moved here. re-named variable
		String title = new String("Tech[//w//W]*"); //moved here
		StringBuffer isolateBufferLeft = new StringBuffer(); //moved here
		StringBuffer isolateBufferRight = new StringBuffer();
		
		try {
		   RE document = new RE(pageHtml); //never used
		   //define the left and right isolator.
		   //String sLeft = new String("MORE FROM CNN[//w//W]*"); // moved. if it defines both, why labeled left?
		   RE leftContext = new RE(title);
		   RE rightContext = new RE("><b>Tech");
		   //StringBuffer sLIsolator = new StringBuffer(""); unnecessary
		   //int iLIsolatorIndex = 0; moved
		   //StringBuffer sLIsolator = new StringBuffer(); //moved. terrible var name.
		   //RE regLIsolater = new RE(leftContext);
		   
		   //handle the left side
		   RE leftIsolator = new RE(leftContext);
		   REMatch contextMatchLeft = leftIsolator.getMatch(pageHtml);
		   isolateBufferLeft.append(contextMatchLeft.toString());
		   isolateLeftIndex = contextMatchLeft.getStartIndex();
		   
		   //handle the rigth side
		   //StringBuffer sRIsolator = new StringBuffer(); moved to the top
		   RE rightIsolator = new RE(rightContext);
		   //int iRIsolator = 0; moved to the top
		   REMatch contextMatchRight = rightIsolator.getMatch(pageHtml);
		   isolateBufferRight.append(contextMatchRight.toString());
		   isolateRightIndex = contextMatchRight.getStartIndex();
		   
		   //delete from the page
		   pageHtml.delete(isolateRightIndex, pageHtml.length());
		   pageHtml.delete(0,isolateLeftIndex);
		   //return(pageHtml) need to catch exceptions first
		}
		catch (REException e) {
				System.out.println("RE exception");
		}
		
		return pageHtml;
	}

}
